package com.lzy.managersystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzy.managersystem.controller.dto.UserDTO;
import com.lzy.managersystem.exception.ServiceException;
import com.lzy.managersystem.mapper.RoleMapper;
import com.lzy.managersystem.mapper.RoleMenuMapper;
import com.lzy.managersystem.mapper.UserMapper;
import com.lzy.managersystem.pojo.Menu;
import com.lzy.managersystem.pojo.Role;
import com.lzy.managersystem.pojo.User;
import com.lzy.managersystem.service.IUserService;
import com.lzy.managersystem.service.MenuService;
import com.lzy.managersystem.tools.Constans;
import com.lzy.managersystem.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class IUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Autowired
    private MenuService menuService;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;

    private static final Log LOG = Log.get();

    @Override
    public UserDTO login(UserDTO userDTO) {
        User one = getUserInfo(userDTO);
        if (one != null) {
            BeanUtil.copyProperties(one, userDTO, true);
            String token = TokenUtils.getToken(one.getId().toString(), one.getPassword());
            userDTO.setToken(token);
//                String role= one.getRole(); //获取当前用户的角色
//
//                //查询用户所有菜单
//                List<Menu> roleMenus = getRoleMenus(role);

            //MenuServiceImpl的获取当前用户的权限菜单逻辑
            List<Menu> roleMenus = menuService.getUserMenu(userDTO.getUsername());


            userDTO.setMenus(roleMenus);
            return userDTO;
        } else {
                throw new ServiceException(Constans.CODE_600, "用户名或密码错误");
            }
    }

    @Override
    public User register(UserDTO userDTO) {
            User one = getUserInfo(userDTO);
            if (one == null) {
                one = new User();
                BeanUtil.copyProperties(userDTO, one, true);
                save(one);  // 把 copy完之后的用户对象存储到数据库
            } else {
                throw new ServiceException(Constans.CODE_600, "用户已存在");
            }
            return one;
        }
        private User getUserInfo(UserDTO userDTO) {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username", userDTO.getUsername());
            queryWrapper.eq("password", userDTO.getPassword());
            User one;
            try {
                one = getOne(queryWrapper); // 从数据库查询用户信息
            } catch (Exception e) {
                LOG.error(e);
                throw new ServiceException(Constans.CODE_500, "系统错误");
            }
            return one;
        }

    /**
     * 获取当前角色的菜单列表
     *
     * @param keyname
     * @return
     */
    private List<Menu> getRoleMenus(String keyname) {
        Role roleid = roleMapper.selectByKeyname(keyname);

        List<Integer> menuIds = roleMenuMapper.selectByRoleId(roleid.getId());
        //查询系统所有菜单
        List<Menu> allMenu = menuService.getAllMenu(null);
        for (Menu menu : allMenu) {
            System.out.println(menu);
        }

        /**
         *  new一个最后筛选完成之后的list
         *  menuList是当前用户的菜单id集合
         */
        List<Menu> roleMenus = new ArrayList<Menu>();
        // 筛选当前用户角色的菜单
        for (Menu menu : allMenu) {
            if (menuIds.contains(menu.getId())) {
                roleMenus.add(menu);
            }
//            if(menuIds.equals(menu.getId())){
//                roleMenus.add(menu);
//            }
            List<Menu> children = menu.getChildren();
            // removeIf()  移除 children 里面不在 menuIds集合中的 元素
            children.removeIf(child -> !menuIds.contains(child.getId()));
        }


        if (roleMenus.isEmpty()) {
            System.out.println("空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空空");
        }
        return roleMenus;

    }

}

